layui.define(['table', 'form', 'laypage', 'laydate', 'layer'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , laydate = layui.laydate
        , laypage = layui.laypage
        , layer = layui.layer;

    //路径
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPaht = curWwwPath.substring(0, pos);
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);


    // alert(departmenttid)

    var data=[{title: 'ID', width: 70, sort: true, fixed: 'left', type: 'numbers'},
        {field: 'title', title: '主题', sort: true},
        {field: 'complete', title: '审核是否结束', sort: true},
        {field: 'urgent', title: '紧急程度', sort: true},
        {field: 'type', title: '审核类型', sort: true,templet: '#titleTpl'},
        {field: 'starttimestring', title: '开始时间', sort: true},
        {field: 'endtimestring', title: '结束时间', sort: true},
        {title: '操作', toolbar: '#table-apply', align: "center", width: 300, fixed: "right"}]

    var limitcount = 10;
    table.render({
        elem: '#applyTable'
        , url: localhostPaht + projectName + "/rest/process/getProcessAllList"
        , title: "流程列表"
        , height: 'full-210'
        , page: false
        , limit: limitcount
        , text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        },
        defaultToolbar: ['filter']
        // ,skin: 'line' //表格风格
        , cols: [data],
        done: function (res, curr, count) {
            $('.layui-table-fixed-r').removeClass('layui-hide');//列固定右侧
            var curnum = $("#curnum").val();
            laypage.render({
                elem: 'laypage'
                , first: '首页'
                , last: '尾页'
                , groups: 5
                , count: count
                , curr: curnum
                , limit: limitcount
                , layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
                , limits: [10, 15, 20]
                , jump: function (obj, first) {
                    if (!first) {
                        curnum = obj.curr;
                        $("#curnum").val(curnum)
                        limitcount = obj.limit;
                        table.reload('applyTable', { //表格的id
                            where: {
                                page: curnum,
                                limit: limitcount
                            }
                        })
                    }
                }
            })
        }
    });

    //监听工具条
    table.on('tool(applyTables)', function (obj) {  //表格的lay-filter="userlists"
        var data = obj.data; //获得当前行所有数据
        // console.log(data) //当前容器的全部表单字段，名值对形式：{name: value}
        // alert(data.vip)
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        // JSON.stringify(data)
        if (obj.event === 'confirm') {
            confirm(data);
        } else if (obj.event == 'info') {
            info(data);
        } else if(obj.event == 'process'){
            process(data);
        }
    });

    function process(data){
        var clocess = "1";
        layer.open({
            title: "审批过程",
            type: 2,
            content: localhostPaht + projectName + '/rest/getRoute?type=instrument&url=apply_process',
            //是否显示右上角关闭图标，默认为1，显示
            area: ['90%', '91%'],
            maxmin: true,
            shade: 0.6,
            anim: 4,
            skin: 'layui-layer-molv',
            resize: true,
            success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                var iframe = window['layui-layer-iframe' + index];
                iframe.child(data.tid)
            }, cancel: function (index, layero) {
                layer.close(index)
                clocess = "2"
                return false;
            },
            end: function () {
                if (clocess == "2") {

                } else {
                    table.reload('applyTable')
                }
                return false;
            }
        });
    }

    function info(data){

        var url="";
        var titles="";
        if(data.type==1){
            url='/rest/getRoute?type=instrument&url=apply_info'
            titles='采购清单';
        }else {
            url='/rest/getRoute?type=instrument&url=chemicalcollect_info'
            titles='领用清单';
        }

        var clocess = "1";
        layer.open({
            title:titles,
            type: 2,
            content: localhostPaht + projectName + url,
            //是否显示右上角关闭图标，默认为1，显示
            area: ['90%', '91%'],
            maxmin: true,
            shade: 0.6,
            anim: 4,
            skin: 'layui-layer-molv',
            resize: true,
            success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                var iframe = window['layui-layer-iframe' + index];
                iframe.child(data.tid,data.batchnumber)
            }, cancel: function (index, layero) {
                layer.close(index)
                clocess = "2"
                return false;
            },
            end: function () {
                if (clocess == "2") {

                } else {
                    table.reload('applyTable')
                }
                return false;
            }
        });
    }

    function confirm(data){
        layer.confirm('是否确认入库', {icon: 3, title: '提示信息', shade: 0.6}, function (index) {
            layer.close(index);
            layer.msg('入库中，请稍候', {icon: 16, time: false, shade: 0.6});
            setTimeout(function () {
                //向服务端发送删除指令
                $.ajax({
                    type: "post",
                    url: localhostPaht + projectName + "/rest/process/addWarehousing",
                    data: "tid="+data.tid+"&batchnumber=" + data.batchnumber,
                    dataType: "json",
                    async: false,
                    success: function (msg) {
                        layer.close(index);
                        if (msg == true) {
                            layer.msg('入库成功');
                            table.reload('applyTable');
                        } else {
                            layer.msg(msg.errorMsg);
                        }
                    }
                })
            }, 2000);
        });
    }



    exports('apply', {})
});
